﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class DESEncryptorHelper
    {
        // Fields
        private const string DefaultDesKey = "auto@#$&";
        private static readonly byte[] DefaultIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef, 0xa9, 0x3a, 0xcf, 0xae, 0xb1, 240, 0xf3, 0x9b };

        // Methods
        private static DESCryptoServiceProvider CreateDESProvider(string encryptKey)
        {
            DESCryptoServiceProvider provider;
            int length = encryptKey.Length;
            if (length < 8)
            {
                for (int i = 0; i < (8 - length); i++)
                {
                    encryptKey = encryptKey + "@";
                }
            }
            provider = new DESCryptoServiceProvider();
            provider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, provider.KeySize/8));
            byte[] destinationArray = new byte[provider.BlockSize / 8];
            Array.Copy(DefaultIV, 0, destinationArray, 0, destinationArray.Length);
            provider.IV = destinationArray;
            return provider;
        }

        public static string Decrypt(string encryptedText)
        {
            return Decrypt(encryptedText, "auto@#$&");
        }

        public static string Decrypt(string encryptedText, string decryptKey)
        {
            if (StringHelper.IsEmptyString(encryptedText))
            {
                return encryptedText;
            }
            DESCryptoServiceProvider provider = CreateDESProvider(decryptKey);
            byte[] buffer = Convert.FromBase64String(encryptedText);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return new UTF8Encoding().GetString(stream.ToArray());
        }

        public static void DecryptFile(string inputFilePath, string outFilePath)
        {
            DecryptFile(inputFilePath, outFilePath, "auto@#$&");
        }

        public static void DecryptFile(string inputFilePath, string outFilePath, string decryptKey)
        {
            FileStream stream = null;
            FileStream stream2 = null;
            CryptoStream stream3 = null;
            try
            {
                stream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read);
                stream2 = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
                stream2.SetLength(0L);
                byte[] buffer = new byte[100];
                long num = 0L;
                long length = stream.Length;
                DES des = CreateDESProvider(decryptKey);
                stream3 = new CryptoStream(stream2, des.CreateDecryptor(), CryptoStreamMode.Write);
                while (num < length)
                {
                    int count = stream.Read(buffer, 0, 100);
                    stream3.Write(buffer, 0, count);
                    num += count;
                }
            }
            finally
            {
                if (stream3 != null)
                {
                    stream3.Close();
                }
                if (stream2 != null)
                {
                    stream2.Close();
                }
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }

        public static string Encrypt(string strText)
        {
            return Encrypt(strText, "auto@#$&");
        }

        public static string Encrypt(string strText, string encryptKey)
        {
            if (StringHelper.IsEmptyString(strText))
            {
                return strText;
            }
            DESCryptoServiceProvider provider = CreateDESProvider(encryptKey);
            byte[] bytes = Encoding.UTF8.GetBytes(strText);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            return Convert.ToBase64String(stream.ToArray());
        }

        public static void EncryptFile(string inputFilePath, string outFilePath)
        {
            EncryptFile(inputFilePath, outFilePath, "auto@#$&");
        }

        public static void EncryptFile(string inputFilePath, string outFilePath, string encryptKey)
        {
            FileStream stream = null;
            FileStream stream2 = null;
            CryptoStream stream3 = null;
            try
            {
                stream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read);
                stream2 = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
                stream2.SetLength(0L);
                byte[] buffer = new byte[100];
                long num = 0L;
                long length = stream.Length;
                DES des = CreateDESProvider(encryptKey);
                stream3 = new CryptoStream(stream2, des.CreateEncryptor(), CryptoStreamMode.Write);
                while (num < length)
                {
                    int count = stream.Read(buffer, 0, 100);
                    stream3.Write(buffer, 0, count);
                    num += count;
                }
            }
            finally
            {
                if (stream3 != null)
                {
                    stream3.Close();
                }
                if (stream2 != null)
                {
                    stream2.Close();
                }
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }

        public static string MD5Hash(string strText)
        {
            return MD5Hash(strText, Encoding.Default);
        }

        public static string MD5Hash(string strText, Encoding encoding)
        {
            MD5 md = new MD5CryptoServiceProvider();
            byte[] bytes = encoding.GetBytes(strText);
            byte[] buffer2 = md.ComputeHash(bytes);
            string str = null;
            for (int i = 0; i < buffer2.Length; i++)
            {
                string str2 = buffer2[i].ToString("x");
                if (str2.Length == 1)
                {
                    str2 = "0" + str2;
                }
                str = str + str2;
            }
            return str;
        }
    }
}
